package com.kmdd.xtds

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.navigation.ui.AppBarConfiguration
import com.kmdd.xtds.databinding.ActivityMainBinding
import com.kmdd.xtds.ui.FourFragment
import com.kmdd.xtds.ui.HealthyFragment
import com.kmdd.xtds.ui.SecondFragment
import com.kmdd.xtds.ui.ThridFragment
import com.gyf.immersionbar.ktx.immersionBar

class MainActivity : AppCompatActivity() {

    private lateinit var binding: ActivityMainBinding

    var healthyFragment: HealthyFragment? = null
    var secondFragment: SecondFragment? = null
    var thirdFragment: ThridFragment? = null
    var fourFragment: FourFragment? = null
    var currentFragment: Fragment? = null
    var mainTabIndex = 0

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)
        immersionBar {
            transparentBar()
            keyboardEnable(true)
            fitsSystemWindows(false)
            statusBarDarkFont(false)
        }
        initFragment()
    }

    fun initFragment() {
        binding.bottomNavigationView.itemIconTintList = null
        binding.bottomNavigationView.setOnNavigationItemReselectedListener {
        }
        binding.bottomNavigationView.setOnNavigationItemSelectedListener { it ->
            when (it.order) {
                0 -> {
                    if (healthyFragment == null) {
                        healthyFragment = HealthyFragment()
                    }

                    switchContent(currentFragment, healthyFragment)

                    mainTabIndex = it.order
                    return@setOnNavigationItemSelectedListener true
                }

                1 -> {
                    if (secondFragment == null) {
                        secondFragment = SecondFragment()
                    }

                    switchContent(currentFragment, secondFragment)

                    mainTabIndex = it.order
                    return@setOnNavigationItemSelectedListener true
                }

                2 -> {
                    if (thirdFragment == null) {
                        thirdFragment = ThridFragment()
                    }

                    switchContent(currentFragment, thirdFragment)
                    mainTabIndex = it.order
                    return@setOnNavigationItemSelectedListener true
                }

                3 -> {
                    if (fourFragment == null) {
                        fourFragment = FourFragment()
                    }

                    switchContent(currentFragment, fourFragment)
                    mainTabIndex = it.order
                    return@setOnNavigationItemSelectedListener true

                }
            }
            mainTabIndex = it.order
            return@setOnNavigationItemSelectedListener false
        }


        //判断activity是否重建，如果不是，则不需要重新建立fragment.
        if (healthyFragment == null) {
            healthyFragment = HealthyFragment()
        }
        if (currentFragment == null) {
            currentFragment = healthyFragment
        }

        supportFragmentManager.beginTransaction()
            .replace(R.id.fragments, currentFragment ?: healthyFragment!!)
            .commit();//fragment parent layout id
    }

    fun switchContent(from: Fragment?, to: Fragment?) {
        if (from == null || to == null) return;
        if (currentFragment != to) {
            currentFragment = to;
            if (!to.isAdded) {
                //fragment parent layout id
                supportFragmentManager.beginTransaction().hide(from).add(R.id.fragments, to)
                    .commit();
            } else {
                supportFragmentManager.beginTransaction().hide(from).show(to).commit();
            }
        }
    }
}